package com.shop.cereshop.agent.service.user.Impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.agent.dao.user.CereAgentUserRoleDAO;
import com.shop.cereshop.agent.service.user.CereAgentRoleService;
import com.shop.cereshop.agent.service.user.CereAgentUserRoleService;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.agent.CereAgentRole;
import com.shop.cereshop.commons.domain.agent.CereAgentUserRole;
import com.shop.cereshop.commons.utils.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @description: TODO
 * @author lx
 * @date 2025/11/15 17:23
 * @version 1.0
 */
@Service
public class CereAgentUserRoleServiceImpl extends ServiceImpl<CereAgentUserRoleDAO, CereAgentUserRole>
    implements CereAgentUserRoleService {

    @Autowired
    private CereAgentRoleService cereAgentRoleService;
  @Override
  public Integer hasRole(Long agentUserId) {
    List<CereAgentUserRole> list = lambdaQuery()
        .eq(CereAgentUserRole::getAgentUserId, agentUserId)
        .list();
    if (list == null || CollUtil.isEmpty(list)) {
      return null;
    }

    //查询角色信息
    List<CereAgentRole> cereAgentRoles = cereAgentRoleService.lambdaQuery().list();
    // 返回角色状态：0-都有，1-只有代理商，2-只有业务员
    if (list.size() >= 2) {
        return IntegerEnum.HAS_ROLE_ALL.getCode();
    } else if (list.size() == 1) {
        CereAgentUserRole cereAgentUserRole = list.get(0);
        for (CereAgentRole cereAgentRole : cereAgentRoles) {
            if (cereAgentRole.getAgentRoleId().equals(cereAgentUserRole.getAgentRoleId())) {
                return cereAgentRole.getRoleType();
            }
        }
    }
    return null;
  }
}
